﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text.RegularExpressions;

namespace Utility
{
    public class StringHelper
    {
        DataRow __common_data_row;

        public void StringFormat8Row(DataRow drowFill, DataSet dsetSource, Dictionary<string, object> mergeDict)
        {
            Regex reg = new Regex(@"\{\s*(\w+)\s*\}");
            List<string> lstMerge = new List<string>();
            DataTable dtblSource = dsetSource.Tables[mergeDict["CHILD_TABLE_NAME"].ToString()];
            string filterFormual = "1=1";
            if (mergeDict.ContainsKey("FILTER_FIELD"))
            {
                object filter_value = drowFill[mergeDict.ContainsKey("FILTER_FIELD_SUB") ? mergeDict["FILTER_FIELD_SUB"].ToString() : mergeDict["FILTER_FIELD"].ToString()];

                filterFormual = string.Format("{0}='{1}'", mergeDict["FILTER_FIELD"], filter_value);
            }
            DataRow[] drowFilter = dtblSource.Select(filterFormual);
            string mergeFormat = mergeDict["MERGE_FORMAT"].ToString();
            foreach (DataRow drowSource in drowFilter)
            {
                __common_data_row = drowSource;
                lstMerge.Add(reg.Replace(mergeFormat, new MatchEvaluator(ReplaceRegex)));
            }

            string target_field = mergeDict["TARGET_FIELD"].ToString();
            string split_char = mergeDict["SPLIT_CHAR"].ToString();

            drowFill[target_field] = string.Join(split_char, lstMerge.ToArray());
        }

        private string ReplaceRegex(Match m)
        {
            string text = "";
            if (!m.Success)
            {
                return text;
            }
            string field_name = m.Groups[1].Value;
            if (__common_data_row.IsNull(field_name))
            {
                return text;
            }

            if (__common_data_row.Table.Columns[field_name].DataType.Equals(typeof(DateTime)))
            {
                return string.Format("{0:yyyy-MM-dd}", __common_data_row[field_name]);
            }
            else if (__common_data_row.Table.Columns[field_name].DataType.Equals(typeof(Decimal)))
            {
                return string.Format("{0:F4}", __common_data_row[field_name]);
            }
            else
            {
                return __common_data_row[field_name].ToString();
            }
        }

        public string StringFormatByData(string format,DataRow drowFill)
        {  //  \{\s*(\w+):*\s*\}|
            Regex reg = new Regex(@"\{\s*(\w+)\s*:*\s*([^{}]+)\s*\}");
            __common_data_row = drowFill;
            return reg.Replace(format, new MatchEvaluator(ReplaceStringFormatRegex));
        }

        private string ReplaceStringFormatRegex(Match m)
        {
            string text = "";
            if (!m.Success)
            {
                return text;
            }
            string field_name = m.Groups[1].Value;
            if (__common_data_row.IsNull(field_name))
            {
                return text;
            }
            string format = "";
            if (m.Groups.Count > 2)
            {
                format = m.Groups[2].Value.Trim();
            }
            object obj = __common_data_row[field_name];
            if (string.IsNullOrEmpty(format))
            {
                if (__common_data_row.Table.Columns[field_name].DataType.Equals(typeof(DateTime)))
                {
                    format = "{0:yyyy-MM-dd}";
                    DateTime currentDate = (DateTime)obj;
                    if (currentDate <= DateTime.Parse("1900-01-30"))
                    {
                        format = "";
                    }
                }
                else if (__common_data_row.Table.Columns[field_name].DataType.Equals(typeof(Decimal)))
                {
                    format = "{0:F2}";
                }
                else
                {
                    format = "{0}";
                }
            }
            else {
                format = "{0:" + format.Trim(':') + "}";
            }
            return string.Format(format, obj);
        }
    }
}